package com.apac.services;

import java.util.Date;

import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.editor.DateCellEditor;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;

import com.apac.jmesa.JmesaObjectList;
import com.apac.jmesa.RowEventJmesa;

public class TableGeneratorService extends AbstractService {

	public static HtmlTable generateHtmlTable(JmesaObjectList items, RowEventJmesa reJmesa) {
		HtmlTable htmlTable = new HtmlTable();

		HtmlRow htmlRow = new HtmlRow();

		htmlRow.setOnclick(reJmesa);
		htmlTable.setRow(htmlRow);
		
		for (String key : items.getKeys()) {
			HtmlColumn column = new HtmlColumn(key).title(key);
			
			if (items.size() > 0) {
				if (items.get(0).get(key) instanceof Date) {
					column.setCellEditor(new DateCellEditor("MMMM dd, yyyy"));
				}
			}
			htmlRow.addColumn(column);
		}

		return htmlTable;
	}

	public static Table generateExportTable(JmesaObjectList list) {
		Table table = new Table();

		Row row = new Row();
		table.setRow(row);

		for (String key : list.getKeys()) {
			Column column = new Column(key).title(key);
			if (list.size() > 0) {
				if (list.get(0).get(key) instanceof Date) {
					column.setCellEditor(new DateCellEditor("MMMM dd, yyyy"));
				}
			}
			row.addColumn(column);
		}

		return table;
	}

}
